package com.kun.kunkunmall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestThread {
    public static ExecutorService service = Executors.newFixedThreadPool(100);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main start ..........");
//        CompletableFuture.runAsync(()->{
//            System.out.println("当前线程"+Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果"+i);
//        },service);
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("运行结果" + i);
//            return i;
//        }, service).whenCompleteAsync((res,exception)->{
//            // 虽然能得到异常信息，但是没办法修改返回数据
//            System.out.println("异步任务成功了，结果是"+res+"一场是"+exception);
//        }).exceptionally(throwable -> {
//            return 10;
//        });

        // 方法执行完成后的处理
//        CompletableFuture<Object> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("运行结果" + i);
//            return i;
//        }, service).handle((res, exception) -> {
//            if (res != null) {
//                return res * 2;
//            }
//            if (exception != null) {
//                return 0;
//            }
//            return 0;
//        });

        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果" + i);
            return i;
        }, service).thenApplyAsync((res) -> {
            System.out.println("第二个线程启动了：res=" + res);
            return "hello" + res;
        }, service);
//        System.out.println(future.get());
        System.out.println("main end ..........");
    }
}
